package com.resou.reader.mine.vipcenter;

import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.MonthlyVipBean;
import com.resou.reader.api.entry.PlateMoreBean;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.service.UserService;
import com.resou.reader.api.service.VipService;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.data.bookstore.model.BasicList;
import com.resou.reader.data.bookstore.model.PlateNovels;
import com.resou.reader.mine.IView.UserInstance;
import com.resou.reader.mine.vipcenter.adapter.MonthlyVipGridAdapter;
import com.resou.reader.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Author: 萤火虫
 * E-mail:  Allensuns@163.com
 * Description: 包月VIP presenter层
 * CreateTime: 2019-04 -15
 **/
public class MonthlyVipPresenter extends ResouBasePresenter<IMonthlyVipContract.IMonthlyVipView> implements IMonthlyVipContract.IMonthlyVipPresenter {
    private static final String TAG = "MonthlyVipPresenter";
    VipService vipService;
    UserService userService;
    List<MonthlyVipBean> monthlyVipBeans;

    public MonthlyVipPresenter(IMonthlyVipContract.IMonthlyVipView view) {
        super(view);
        vipService = ApiImp.getInstance().getService(VipService.class);
        userService = ApiImp.getInstance().getService(UserService.class);
    }


    /**
     * 请求包月vip显示数据
     */
    @Override
    public void refreshData() {
        mView.showProgress();
        addCompositeDisposable(Observable.zip(
                vipService.getVIPBanner(2).subscribeOn(Schedulers.io()),
                userService.getUserInfo(UserInstance.getToken()).subscribeOn(Schedulers.io()),
                vipService.getVIPPrivilege().subscribeOn(Schedulers.io()),
                vipService.getVIPPlateList("vip").subscribeOn(Schedulers.io()),
                (commonDataResult, userInfo, privilegeBeanResult, basicListResultList) -> {
                    /**
                     * 轮播图
                     * 用户信息
                     */
                    monthlyVipBeans = new ArrayList<>();
                    MonthlyVipBean banner = new MonthlyVipBean(MonthlyVipBean.HEADER);
                    banner.setList(commonDataResult.getData().getData());
                    banner.setUserInfoBean(userInfo.getData());
                    monthlyVipBeans.add(banner);

                    /**
                     * 特权数据
                     */
                    MonthlyVipBean privilege = new MonthlyVipBean(MonthlyVipBean.PRIVILEGE);
                    privilege.setPrivilegeList(privilegeBeanResult.getData());
                    monthlyVipBeans.add(privilege);
                    /**
                     * 书库数据
                     */
                    if (basicListResultList.getData() != null && basicListResultList.getData().size() > 0) {
                        List<BasicList<PlateNovels>> lists = basicListResultList.getData();
                        for (BasicList<PlateNovels> basic : lists) {
                            /**
                             * 1:小编力荐和惊艳完本展示类型
                             * 2:会员抢先和分类(都市、玄幻等)
                             * 3: VIP专享畅读
                             *
                             */
                            if (basic.getPlateShowType() == 3) {
                                MonthlyVipBean updata = new MonthlyVipBean(MonthlyVipBean.UPDATAGRID);
                                updata.setPlateNovelList(basic);
                                monthlyVipBeans.add(updata);
                            } else if (basic.getPlateShowType() == 2) {
                                MonthlyVipBean updata = new MonthlyVipBean(MonthlyVipBean.RECOMMENDGRID);
                                updata.setPlateNovelList(basic);
                                monthlyVipBeans.add(updata);
                            } else {
                                MonthlyVipBean updata = new MonthlyVipBean(MonthlyVipBean.RECOMMENDLIST);
                                updata.setPlateNovelList(basic);
                                monthlyVipBeans.add(updata);
                            }
                        }
                    }
                    return monthlyVipBeans;
                }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<MonthlyVipBean>>() {
                    @Override
                    public void accept(List<MonthlyVipBean> list) throws Exception {
                        if (list != null) {
                            mView.showData(list);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
//                        ToastUtil.makeShortToast(throwable.getMessage());
                        mView.showError(throwable);
                    }
                }));
    }

    /***
     *
     * @param plateId       板块ID：榜单列表接口返回的plateId
     * @param plateViewType 查看类型：榜单列表接口返回的plateViewType
     * @param page          页数：从0开始，默认为0
     */
    @Override
    public void refreshModule(MonthlyVipGridAdapter monthlyVipGridAdapter, String plateId, String plateViewType, int page, int position) {
        vipService.getVIPPlateMore(plateId, plateViewType, page, 4).subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers())
                .subscribe(new Consumer<Result<PlateMoreBean<PlateNovels>>>() {
                    @Override
                    public void accept(Result<PlateMoreBean<PlateNovels>> plateMoreBeanResult) throws Exception {
                        if (plateMoreBeanResult.getData() != null && plateMoreBeanResult.getData().getPlateMore().size() > 0) {
                            monthlyVipGridAdapter.setNewData(plateMoreBeanResult.getData().getPlateMore());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });
    }


}
